package com.itheima.app.log;

import cn.hutool.core.date.DateUtil;
import com.itheima.app.interceptor.UserHolder;
import com.itheima.domain.db.Log;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

@Aspect
@Component
//aop类
public class LogAspect {
    @Autowired
    private RocketMQTemplate rocketMQTemplate;
    //环绕通知
    @Around(value = "@annotation(logWith)")//出现logWith注解时运行此程序
    public Object aroundLog(ProceedingJoinPoint pjp,LogWith logWith) throws Throwable {
        //创建日志类 保存创建发布动态记录
        Log log = new Log();
        log.setUserId(UserHolder.get().getId());
        log.setLogTime(DateUtil.formatDate(new Date()));
        log.setPlace("黑马程序员");
        log.setEquipment("华为");
        log.setType(logWith.type());
        //把log发送到消息中间件
        rocketMQTemplate.convertAndSend("tanhua-user-log", log);
        Object proceed = pjp.proceed();
        return proceed;
    }
}
